package main

import (
	"fmt"
	"os"

	"github.com/urfave/cli/v2"
)

type Config struct {
	AddrDev  string
	AddrUser string
}

func runServer(c *cli.Context) {

	cfg := &Config{
		AddrDev:  "0.0.0.0:9011",
		AddrUser: "0.0.0.0:9012",
	}

	br := newBroker(cfg)
	listenDevice(br)

	go br.run()
	select {}
}

func main() {
	app := &cli.App{
		Name:    "XZRobot Ops Server",
		Usage:   "The Server Side For xzrobot ops",
		Version: "1.0.0",
		Commands: []*cli.Command{
			{
				Name:  "run",
				Usage: "Run Server",
				Flags: []cli.Flag{
					&cli.StringFlag{
						Name:  "log",
						Value: "log.txt",
						Usage: "log file path",
					},
					&cli.StringFlag{
						Name:    "conf",
						Aliases: []string{"c"},
						Value:   "./rttys.conf",
						Usage:   "config file to load",
					},
					&cli.StringFlag{
						Name:  "addr-dev",
						Value: ":9011",
						Usage: "address to listen device",
					},
					&cli.StringFlag{
						Name:  "addr-user",
						Value: ":9012",
						Usage: "address to listen user",
					},
					&cli.StringFlag{
						Name:  "db",
						Value: "sqlite://database.db",
						Usage: "database source",
					},
				},
				Action: func(c *cli.Context) error {
					runServer(c)
					return nil
				},
			},
		},
		Action: func(c *cli.Context) error {
			c.App.Command("run").Run(c)
			return nil
		},
	}

	err := app.Run(os.Args)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
